package de.mnit.basis.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 * @author Michael Nitsche
 * 23.06.2010	Erstellt
 * MIT DOM auswerten
 * 
 * !!! Nicht Funktionstüchtig !!!
 */
public class XMLLeser {

	private final DocumentBuilder builder;
	private Document xml=null;

	
	public XMLLeser() throws ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
		builder = factory.newDocumentBuilder();
	}
	
	
	public void lies(InputStream is) throws SAXException, IOException {
//	    Document document = builder.parse( new File(datei) ); 
	    this.xml = builder.parse(is); 
	    System.out.println( xml.getFirstChild().getTextContent() ); 
	}
	
	public void lies(File datei) throws SAXException, IOException {
	    this.xml = builder.parse(datei);
//	    this.xml.getDocumentElement ().normalize ();
	    this.xml.normalize();
	    System.out.println ("Root element of the doc is " + 
                xml.getDocumentElement().getNodeName());

	    System.out.println( xml.getFirstChild().getTextContent() ); 
	}
	
	
	public NodeList gEbene(String... pfad) {
//		NodeList list=this.xml.getChildNodes();
		NodeList list=null;
		for(String s : pfad)
			list=this.xml.getElementsByTagName(s);
		
//		XPathFactory xpathFactory = XPathFactory.newInstance();
//		XPath xpath = xpathFactory.newXPath();
//		NodeList links = (NodeList) xpath.evaluate("rootNode/link", element,
//		    XPathConstants.NODESET);

//		Element parent=this.xml.getDocumentElement();
		
//		
//		 List<Element> nodeList = new ArrayList<Element>();
//		    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
//		      if (child.getNodeType() == Node.ELEMENT_NODE && 
//		          name.equals(child.getNodeName())) {
//		        nodeList.add((Element) child);
//		      }
//		    }
//
//		    return nodeList;

		
		
//	    xml.getElementsByTagNameNS(namespaceURI, localName)

		return list;
	}

}
